package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.PlayerGameLibraryDao;
import com.zwxbc.funplay.entity.PlayerGameLibrary;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PlayerGameLibraryDaoImpl implements PlayerGameLibraryDao {
	private static final String INSERT_LIBRARY = "INSERT INTO player_game_library (player_id, game_id, purchase_date, playtime) VALUES (?, ?, ?, ?)";
	private static final String SELECT_LIBRARY_BY_ID = "SELECT * FROM player_game_library WHERE library_id = ?";
	private static final String SELECT_ALL_LIBRARIES = "SELECT * FROM player_game_library";
	private static final String UPDATE_LIBRARY = "UPDATE player_game_library SET player_id = ?, game_id = ?, purchase_date = ?, playtime = ? WHERE library_id = ?";
	private static final String DELETE_LIBRARY = "DELETE FROM player_game_library WHERE library_id = ?";
	private static final String SELECT_LIBRARY_BY_PLAYER_ID = "SELECT * FROM player_game_library WHERE player_id = ?";
	private static final String PLAYER_IF_HAVE_GAME = "SELECT * FROM  player_game_library WHERE player_id = ? AND game_id = ?";
	private static final String SELECT_GAME_BY_PLAYER_ID = "SELECT * FROM player_game_library WHERE player_id = ?";
	@Override
	public int addPlayerGameLibrary(PlayerGameLibrary library) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_LIBRARY)) {
			preparedStatement.setLong(1, library.getPlayerId());
			preparedStatement.setLong(2, library.getGameId());
			preparedStatement.setDate(3, library.getPurchaseDate() != null ? new java.sql.Date(library.getPurchaseDate().getTime()) : null);
			preparedStatement.setLong(4, library.getPlaytime());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public PlayerGameLibrary getPlayerGameLibraryById(Long libraryId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_LIBRARY_BY_ID)) {
			preparedStatement.setLong(1, libraryId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createLibraryFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<PlayerGameLibrary> getGameListByPlayerId(Long playerId) {
		List<PlayerGameLibrary> libraries = new ArrayList<>();

		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_LIBRARY_BY_PLAYER_ID)) {
			// 设置查询参数
			preparedStatement.setLong(1, playerId);

			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					libraries.add(createLibraryFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}

		return libraries;
	}

	@Override
	public List<PlayerGameLibrary> getAllPlayerGameLibraries() {
		List<PlayerGameLibrary> libraries = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_LIBRARIES);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				libraries.add(createLibraryFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return libraries;
	}

	@Override
	public int updatePlayerGameLibrary(PlayerGameLibrary library) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_LIBRARY)) {
			preparedStatement.setLong(1, library.getPlayerId());
			preparedStatement.setLong(2, library.getGameId());
			preparedStatement.setDate(3, library.getPurchaseDate() != null ? new java.sql.Date(library.getPurchaseDate().getTime()) : null);
			preparedStatement.setLong(4, library.getPlaytime());
			preparedStatement.setLong(5, library.getLibraryId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deletePlayerGameLibrary(Long libraryId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_LIBRARY)) {
			preparedStatement.setLong(1, libraryId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int playerIfHaveGame(Long gameId, Long playerId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(PLAYER_IF_HAVE_GAME)) {
			preparedStatement.setLong(1, playerId);
			preparedStatement.setLong(2, gameId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return resultSet.getInt(1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public List<PlayerGameLibrary> getPlayerGameLibraryListByPlayerId(Long playerId) {
		List<PlayerGameLibrary> playerGameLibraries = new ArrayList<>();

		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_LIBRARY_BY_PLAYER_ID)) {
			// 设置查询参数
			preparedStatement.setLong(1, playerId);

			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					playerGameLibraries.add(createLibraryFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}

		return playerGameLibraries;
	}

	// Helper 方法，从 ResultSet 中创建 PlayerGameLibrary 对象
	private PlayerGameLibrary createLibraryFromResultSet(ResultSet resultSet) throws SQLException {
		PlayerGameLibrary library = new PlayerGameLibrary();
		library.setLibraryId(resultSet.getLong("library_id"));
		library.setPlayerId(resultSet.getLong("player_id"));
		library.setGameId(resultSet.getLong("game_id"));
		library.setPurchaseDate(resultSet.getDate("purchase_date"));
		library.setPlaytime(resultSet.getLong("playtime"));
		return library;
	}

	public static void main(String[] args) {
		PlayerGameLibraryDao playerGameLibraryDao = new PlayerGameLibraryDaoImpl();
		System.out.println(playerGameLibraryDao.playerIfHaveGame(1L,1L));
	}
}
